﻿using KBWebApi.Common;
using KBWebApi.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KBWebApi.Bll
{
    public class KBBll
    {
        Dal.KBDal kbDal = new Dal.KBDal();

        public async Task<ActionResult> NewOrUpdate(KBInputModel inputModel)
        {
            if (await Task.Run(() => Dal.KBDal.IsExists(inputModel)))
            {
                try
                {
                    var result = await Task.Run(() => kbDal.Update(inputModel));
                    if (result > 0)
                    {
                        return new JsonResult(new
                        {
                            code = 20000,
                            message = "更新成功"
                        });
                    }
                    else
                    {
                        return new JsonResult(new
                        {
                            code = 20002,
                            message = "更新失败"
                        });
                    }
                }
                catch (Exception ex)
                {
                    WriteLog.Writelog("KB.Update=>" + ex.ToString());
                    return new JsonResult(new
                    {
                        code = 20001,
                        message = "发生错误"
                    });
                }
            }
            else
            {
                try
                {
                    inputModel.CreateDate = DateTime.Now;
                    var result = await Task.Run(() => kbDal.NewKBInput(inputModel));
                    if (result > 0)
                    {
                        return new JsonResult(new
                        {
                            code = 20000,
                            message = "新增成功"
                        });
                    }
                    else
                    {
                        return new JsonResult(new
                        {
                            code = 20002,
                            message = "新增失败"
                        });
                    }
                }
                catch (Exception ex)
                {
                    WriteLog.Writelog("KB.New=>" + ex.ToString());
                    return new JsonResult(new
                    {
                        code = 20001,
                        message = "发生错误"
                    });
                }
            }
        }

        public async Task<List<KBInputModel>> GetAllList(KBInputModel inputModel)
        {
            try
            {
                var result = await Task.Run(() => Dal.KBDal.GetAllList(inputModel));
                ModelHandler<KBInputModel> modelHandler = new ModelHandler<KBInputModel>();
                if (result.Rows.Count > 0)
                {
                    var userReturn = modelHandler.FillModel(result);
                    return userReturn;
                }
                return null;
            }
            catch (Exception ex)
            {
                WriteLog.Writelog("KB.GetAllList=>" + ex.ToString());
                return null;
            }
        }

        public async Task<ActionResult> Delete(string KBId)
        {
            try
            {
                var result = await Task.Run(() => kbDal.Delete(KBId));
                if (result > 0)
                {
                    return new JsonResult(new
                    {
                        code = 20000,
                        message = "删除成功"
                    });
                }
                else
                {
                    return new JsonResult(new
                    {
                        code = 20002,
                        message = "删除失败"
                    });
                }
            }
            catch (Exception ex)
            {
                WriteLog.Writelog("KB.Delete=>" + ex.ToString());
                return new JsonResult(new
                {
                    code = 20001,
                    message = "发生错误"
                });
            }
        }

        public async Task<List<string>> GetList(string Name)
        {
            try
            {
                var result = await Task.Run(() => Dal.KBDal.GetList(Name));
                if (result.Rows.Count > 0)
                {
                    List<string> lst = new List<string>();
                    foreach(DataRow dr in result.Rows)
                    {
                        lst.Add(dr[0].ToString());
                    }
                    return lst;
                }
                return null;
            }
            catch (Exception ex)
            {
                WriteLog.Writelog("KB.GetList=>" + ex.ToString());
                return null;
            }
        }
    }
}
